﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Brjostagjof.HelpFunctions
{
    public static class BgExtensions
    {

        /// <summary>
        /// Returns the first n number of letters from a string, adds ... after the last word in a srting to shorten then n
        /// </summary>
        /// <param name="toShorten"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static String DisplayFirst(this string toShorten, int n )
        {
            if (string.IsNullOrEmpty(toShorten))
            {
                return "";
            }
            else if (toShorten.Length > n)
            {
                String temp = toShorten.Remove(n, toShorten.Length - n);
                if (temp.EndsWith(" "))
                {
                    return string.Format("{0}{1}", temp, "...");
                }
                else
                {
                    return temp.DisplayFirst(n - 1);
                }
            }
            else
            {
                return toShorten;
            }
        }
    }
}